package cn.scw.project.system.controller;


import cn.scw.common.constant.SysConsts;
import cn.scw.framework.web.controller.BaseController;
import cn.scw.framework.web.domain.AjaxResult;
import cn.scw.project.facility.domain.netework.bean.WxSharingOrderResp;
import cn.scw.project.system.service.ISysPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@Api(tags = "支付")
@RestController
@RequestMapping(SysConsts.CONTEXT_PATH+"/system/pay")
public class PayController extends BaseController {
    @Autowired
    private ISysPayService iSysPayService;

    @PostMapping("/notify/commodity")
    @ApiOperation("商品订单回调")
    public String wxPayNotifyByCommodity(HttpServletRequest request) {
        try {
            String resXml = getResXml(request);
            return iSysPayService.payBackByCommodity(resXml);
        } catch (Exception e) {
            System.out.println("微信手机支付失败:" + e.getMessage());
            return "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
        }
    }

    @PostMapping("/notify/print")
    @ApiOperation("打印回调")
    public String wxPayNotifyByPrint(HttpServletRequest request) {
        try {
            String resXml = getResXml(request);
            return iSysPayService.payBackByPrint(resXml);
        } catch (Exception e) {
            System.out.println("微信手机支付失败:" + e.getMessage());
            return "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
        }
    }

    @GetMapping("/notify/profitsharing/{orderId}")
    @ApiOperation("请求分账")
    public AjaxResult profitsharing(@PathVariable Long orderId) {
        try {
            WxSharingOrderResp resp = iSysPayService.profitsharing(orderId);
            if(resp !=null && "SUCCESS".equals(resp.getReturn_code()) && "SUCCESS".equals(resp.getResult_code())){
                return AjaxResult.success();
            }
            else if(resp == null){
                return AjaxResult.error();
            }
            else if(!"SUCCESS".equals(resp.getReturn_code())){
                return AjaxResult.error(resp.getReturn_msg());
            }
            else if(!"SUCCESS".equals(resp.getResult_code())){
                return AjaxResult.error(resp.getErr_code_des());
            }
        } catch (Exception e) {
            System.out.println("微信手机支付失败:" + e.getMessage());
        }
        return AjaxResult.error();
    }

    @PostMapping("/notify/pay")
    @ApiOperation("充值回调")
    public String wxPayNotifyByPay(HttpServletRequest request) {
        try {
            String resXml = getResXml(request);
            return iSysPayService.payBackByPay(resXml);
        } catch (Exception e) {
            System.out.println("微信手机支付失败:" + e.getMessage());
            return "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
        }
    }

    private static String getResXml(HttpServletRequest request) throws IOException {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }
            } catch (IOException e) {
                System.out.println(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        return sb.toString();
    }
}
